﻿using PixivVideo.Currency;
using PixivVideo.Currency.IoUtils;
using System;
using System.Collections.Generic;

using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;

namespace PixivVideo.ViewHelper
{
    internal class AnimationHelper
    {
        private List<string> LocalFileList = new List<string>();

        /// <summary>
        /// 播放文件列表
        /// </summary>
        /// <param name="LoadImage"></param>
        /// <param name="ImgList"></param>
        /// <param name="AnimationInSecond"></param>
        /// <param name="AnimationOutSecond"></param>
        /// <param name="TipLable"></param>
        public void ImagePlayer(System.Windows.Controls.Image LoadImage, List<string> ImgList, int AnimationInSecond, int AnimationOutSecond, System.Windows.Controls.Label TipLable, System.Windows.Controls.Label PlayButton)
        {
            LocalFileList = ImgList;
            SendKeys.SendWait("{F2}");
            ImagePlayer(LoadImage, AnimationInSecond, AnimationOutSecond, TipLable, PlayButton);
        }
        /// <summary>
        /// 播放单个文件
        /// </summary>
        /// <param name="LoadImage"></param>
        /// <param name="AnimationInSecond"></param>
        /// <param name="AnimationOutSecond"></param>
        /// <param name="TipLable"></param>
        public void ImagePlayer(System.Windows.Controls.Image LoadImage, int AnimationInSecond, int AnimationOutSecond, System.Windows.Controls.Label TipLable, System.Windows.Controls.Label PlayButton)
        {
           
            if (LocalFileList.Count <= 0) {
                SendKeys.SendWait("{F2}");
                return;
            }
            
            string FilePath = LocalFileList[0];
            LocalFileList.RemoveAt(0);
            LoadImage.Source = GetImageSource(FilePath);
            TipLable.Content ="P站ID："+ TextHelper.ReplaceText(new FileNameHelper().GetFileNameWithoutExtension(FilePath),"_p.*?","");

            PlayButton.Content = "剩余：" + LocalFileList.Count;
            FadeInToOut(LoadImage, AnimationInSecond, AnimationOutSecond, TipLable,PlayButton);
        }

       

        /// <summary>
        /// 播放单个文件
        /// </summary>
        /// <param name="LoadImage"></param>
        /// <param name="ImgPath"></param>
        /// <param name="SleepTime"></param>
        /// <param name="AnimationSecond"></param>
        public void ImagePlayer(System.Windows.Controls.Image LoadImage, string ImgPath, int AnimationInSecond, int AnimationOutSecond, System.Windows.Controls.Label TipLable, System.Windows.Controls.Label PlayButton)
        {
            LoadImage.Source = GetImageSource(ImgPath);
            new AnimationHelper().FadeInToOut(LoadImage, AnimationInSecond, AnimationOutSecond,TipLable, PlayButton);
        }

        private static BitmapImage GetImageSource(string ImgPath)
        {
            var source = new BitmapImage();
            source.BeginInit();
            source.UriSource = new Uri(ImgPath, UriKind.Absolute);
            //设置解码的分辨率大小，也就是搞个缩略图
            source.DecodePixelWidth = 1200;
            //   source.DecodePixelWidth = parameter != null ? int.Parse(parameter.ToString()) : 100;
            source.EndInit();
            return source;
        }

        /// <summary>
        /// 淡入淡出
        /// </summary>
        /// <param name="LoadImage"></param>
        /// <param name="SleepTime"></param>
        /// <param name="AnimationSecond"></param>
        public void FadeInToOut(System.Windows.Controls.Image LoadImage, int AnimationInSecond, int AnimationOutSecond, System.Windows.Controls.Label TipLable, System.Windows.Controls.Label PlayButton)
        {
            //  FadeInImage(LoadImage, AnimationSecond);
            DoubleAnimation daIn = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromSeconds(AnimationInSecond)));
            
            Console.WriteLine("渐入开始"+DateTime.Now.ToString());
            daIn.Completed += delegate
            {
                NewMethod(LoadImage, AnimationInSecond, AnimationOutSecond, TipLable, PlayButton);
            };
            LoadImage.BeginAnimation(UIElement.OpacityProperty, daIn);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="LoadImage"></param>
        /// <param name="AnimationInSecond"></param>
        /// <param name="AnimationOutSecond"></param>
        /// <param name="TipLable"></param>
        private void NewMethod(System.Windows.Controls.Image LoadImage, int AnimationInSecond, int AnimationOutSecond, System.Windows.Controls.Label TipLable, System.Windows.Controls.Label PlayButton)
        {
            new ThreadSleep().SetClock(3,3);
            DoubleAnimation daOut = new DoubleAnimation(1, 0, new Duration(TimeSpan.FromSeconds(AnimationOutSecond)));

            Console.WriteLine("渐入完成，检出开始" + DateTime.Now.ToString());

            // new ThreadSleep().SetClock(3);
            daOut.Completed += delegate
            {
                Console.WriteLine("出去完成" + DateTime.Now.ToString());
                ImagePlayer(LoadImage, AnimationInSecond, AnimationOutSecond,TipLable,PlayButton);
            };
            LoadImage.BeginAnimation(UIElement.OpacityProperty, daOut);
        }

        /// <summary>
        /// 淡入
        /// </summary>
        /// <param name="LoadImage"></param>
        /// <param name="Second"></param>
        public void FadeInImage(System.Windows.Controls.Image LoadImage, int AnimationSecond)
        {
        
            DoubleAnimation daV = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromSeconds(AnimationSecond)));

            LoadImage.BeginAnimation(UIElement.OpacityProperty, daV);
        }

        /// <summary>
        /// 淡出
        /// </summary>
        /// <param name="LoadImage"></param>
        /// <param name="Second"></param>
        public void FadeOutImage(System.Windows.Controls.Image LoadImage, int AnimationSecond)
        {
            DoubleAnimation daV = new DoubleAnimation(1, 0, new Duration(TimeSpan.FromSeconds(AnimationSecond)));
            LoadImage.BeginAnimation(UIElement.OpacityProperty, daV);
        }
    }
}